﻿module RosSharp.GenMsg.Generator

open RosSharp.GenMsg.Ast
open RosSharp.GenMsg.Base
open RosSharp.GenMsg.Parser
open RosSharp.GenMsg.Preprocessor
open RosSharp.GenMsg.MessageGenerator
open RosSharp.GenMsg.ServiceGenerator
open RosSharp.GenMsg.Md5Generator

open FParsec
open System
open System.IO

let getTypeName fileName =
    Path.GetFileNameWithoutExtension(fileName)

let generateHeader =
    let version = Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
    "//----------------------------------------------------------------\r\n" + 
    "// <auto-generated>\r\n" +
    "//     This code was generated by the GenMsg. Version: " + version + "\r\n" +
    "//     Don't change it manually.\r\n" +
    "//     " + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz") + "\r\n" +
    "// </auto-generated>\r\n" + 
    "//----------------------------------------------------------------\r\n"

let saveFile outputDir typeName text = 
    if Directory.Exists(outputDir) = false then
        Directory.CreateDirectory(outputDir) |> ignore
    let fileName = Path.Combine(outputDir, typeName + ".cs")
    File.WriteAllText(fileName, generateHeader + text)
    fileName

let generateMessage (fileName : string) (ns : string) (outputDir : string) (includeDirs : ResizeArray<string>) =
    Md5GeneratorSetting.includeDirectories.AddRange(includeDirs)
    let ast = parseMessageFile fileName
    let typeName = getTypeName fileName
    let text = generateMessageClass ns typeName ast
    saveFile outputDir typeName text


let generateService (fileName : string) (ns : string) (outputDir : string) (includeDirs : ResizeArray<string>) =
    Md5GeneratorSetting.includeDirectories.AddRange(includeDirs)
    let ast = parseServiceFile fileName
    let typeName = getTypeName fileName
    let text = generateServiceClass ns typeName ast
    saveFile outputDir typeName text


